#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from service.userInfoService import UserInfoService
from utils import commons
from flask import jsonify


class UserInfoOtherResource(Resource):
	# 用户登陆
	@classmethod
	def login(cls):
		parser = reqparse.RequestParser()
		parser.add_argument("account", location='form', required=False, help="account参数类型不正确或缺失")
		parser.add_argument("password", location='form', required=False, help="password参数类型不正确或缺失")

		kwargs = parser.parse_args()
		kwargs = commons.put_remove_none(**kwargs)
		result = UserInfoService.login(**kwargs)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])

	# 用户注册
	@classmethod
	def register(cls):
		parser = reqparse.RequestParser()
		parser.add_argument("username", location="form", required=False, help="username参数类型不对或缺失")
		parser.add_argument("account", location="form", required=False, help="account参数类型不对或缺失")
		parser.add_argument("password", location="form", required=False, help="password参数类型不对或缺失")

		# 开始校验
		kwargs = parser.parse_args()
		# 去除None的值
		kwargs = commons.put_remove_none(**kwargs)
		# 使用service层注册逻辑
		result = UserInfoService.register(**kwargs)
		# 调用接口时返回的信息(判定是不是注册成功)
		return jsonify(code=result['code'], message=result['message'], data=result['data'])
